package com.chenchao.javaprocesscontrol;

import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;

import java.util.Scanner;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/6 18:28
 * @Author : Chen Chao
 * @File : javaSEcom.chenchao.javaprocesscontrolJavaScanner.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class JavaScanner {
    public static void main(String[] args) {
        //Scanner scanner = new Scanner(System.in);
        //if (scanner.hasNext()){
            //String str= scanner.next();
            //监测到空格就会停止
            //String str=scanner.nextLine();
            //监测到回车键后停止
            //System.out.println(str);
        //}
        //scanner.close();
        //IO都要手动关闭不然占用资源
        //JavaScanner.test1();
        JavaScanner.test2();
    }
    public static void test1(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入整数");
        if(scanner.hasNextInt()){
            System.out.println("输入数字为:"+scanner.nextInt());
        }
        else {
            System.out.println("输入的不是整数");
        }
        System.out.println("请输入小数");
        if(scanner.hasNextFloat()){
            System.out.println("输入小数为:"+scanner.nextFloat());
        }
        else {
            System.out.println("输入的不是小数");
        }
        scanner.close();

    }
    public static void test2(){
        double sum=0.0;
        int i=0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入小数或整数");
        while (scanner.hasNextDouble()){
            sum+=scanner.nextDouble();
            i++;
            System.out.println("请输入下一个小数或整数");
        }
        System.out.println("输入总和为:"+sum);
        System.out.println("输入平均值为:"+sum/i);
        scanner.close();
    }
}
